package com.binsoft.ndtta;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class NdttaDatabaseAdapter {

	// Database fields
	public static final String KEY_ROWID = "_id";
	public static final String KEY_DATE = "date";
	public static final String KEY_HOME = "home";
	public static final String KEY_AWAY = "away";
	public static final String KEY_DIVISION = "division";
	public static final String KEY_HOMESCORE = "homescore";
	public static final String KEY_AWAYSCORE = "awayscore";
	private Context context;
	private SQLiteDatabase database;
	private NdttaDatabaseHelper dbHelper;

	public NdttaDatabaseAdapter(Context context) 
	{
		Log.i( NdttaDatabaseAdapter.class.getName(), "Creating Database Adapter");
		
		this.context = context;
	}

	public NdttaDatabaseAdapter open() throws SQLException 
	{
		Log.i( NdttaDatabaseAdapter.class.getName(), "Opening database adapter");
		
		dbHelper = new NdttaDatabaseHelper(context);
		
		Log.i( NdttaDatabaseAdapter.class.getName(), "Getting writable database");
		
		database = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() 
	{
		dbHelper.close();
	}

	
	/**
	 * Create a new score If the score is successfully created return the new
	 * rowId for that note, otherwise return a -1 to indicate failure.
	 */

	public long createMatch(Match match ) 
	{
		long created = -1;
		
		try
		{
			ContentValues initialValues = createContentValues(match);
	
			created = database.insert(Constants.DATABASE_TABLE, null, initialValues);
		}
		catch ( Exception e )
		{
			created = -1;
		}
		
		return created;
	}

	
	/**
	 * Update the score
	 */

	public boolean updateMatch( Match match ) 
	{
		boolean success = false;
		
		try
		{
			ContentValues updateValues = createContentValues( match );
	
			success = database.update(Constants.DATABASE_TABLE, updateValues, KEY_DIVISION + "=" + match.division() + " AND " + KEY_HOME + "="
					+ match.home() + " AND " + KEY_AWAY + "=" + match.away(), null) > 0;
		}
		catch ( Exception e )
		{
			success = false;
		}
		
		return success;
	}

	
	/**
	 * Return a Cursor positioned at the defined match
	 */

	public Cursor fetchMatch(Match match, boolean reverse, boolean useMatchDate ) throws SQLException 
	{
		Cursor mCursor = null;
		
		try
		{
			int homeTeam = match.home();
			int awayTeam = match.away();
			
			if ( reverse )
			{
				homeTeam = awayTeam;
				awayTeam = match.home();
			}
			
			String dateQuery = "";
			
			if ( useMatchDate )
			{
				dateQuery = KEY_DATE + "=" + match.date() + " AND ";
			}
			
			mCursor = database.query(true, Constants.DATABASE_TABLE, new String[] {
					KEY_ROWID, KEY_DATE, KEY_DIVISION, KEY_HOME, KEY_AWAY, KEY_HOMESCORE, KEY_AWAYSCORE },
					KEY_DIVISION + "=" + match.division() + " AND " + dateQuery + 
					KEY_HOME + "=" + homeTeam + " AND " + KEY_AWAY + "=" + awayTeam , null, null, null, null, null);
			
			if (mCursor != null)
			{
				mCursor.moveToFirst();
			}
		}
		catch ( Exception e )
		{
			mCursor = null;
		}
		
		return mCursor;
	}

	private ContentValues createContentValues( Match match ) 
	{
		ContentValues values = new ContentValues();
		
		values.put( KEY_DATE, match.date() );
		values.put( KEY_DIVISION, match.division() );
		values.put( KEY_HOME, match.home() );
		values.put( KEY_AWAY, match.away() );
		values.put( KEY_HOMESCORE, match.homeScore() );
		values.put( KEY_AWAYSCORE, match.awayScore() );

		return values;
	}
}
